<template>
  <el-dialog v-model="visible" :title="title">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="7em">
      <el-form-item label="账户" prop="account">
        <el-select v-model="form.account" placeholder="选择账户">
          <el-option
            v-for="item in userProfileStore.list"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="明细类型" prop="type">
        <el-select v-model="form.type">
          <el-option label="入账" value="income" />
          <el-option label="支出" value="outgoing" />
        </el-select>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input v-model="form.amount" placeholder="输入金额" type="number" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="输入备注"
          type="textarea"
          resize="none"
          :rows="5"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="rechargeStore.loading.create || rechargeStore.loading.update"
          @click="handleConfirm"
        >
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { defineModel } from "vue";
import { useAccountStoreHook } from "@/store/modules/user/account";
import { useUserProfileStoreHook } from "@/store/modules/user/profile";
import { AccountForm } from "@/api/user/account";
import { cloneDeep } from "lodash-es";

const rechargeStore = useAccountStoreHook();

const userProfileStore = useUserProfileStoreHook();

console.log(userProfileStore);

export type FormType = "add" | "edit";

export interface Props {
  type: FormType;
  form?: AccountForm;
}

const visible = defineModel({ type: Boolean, default: false });

const emits = defineEmits(["confirm"]);

const props = defineProps<Props>();

const formRef = ref();

const defaultForm: AccountForm = {
  id: undefined,
  account: "",
  type: "income",
  amount: "",
  remark: "",
};

const form = reactive<Partial<AccountForm>>(cloneDeep(defaultForm));

const fileList = ref<any>([]);

const rules = {
  account: [
    {
      required: true,
      message: "请选择账户",
      trigger: "change",
    },
  ],
  type: [
    {
      required: true,
      message: "请选择类型",
      trigger: "change",
    },
  ],
  amount: [
    {
      required: true,
      message: "请输入金额",
      trigger: "change",
    },
  ],
};

const title = computed(() => {
  return props.type === "add" ? "新增" : "编辑";
});

watch(visible, async (val) => {
  if (val) {
    console.log(userProfileStore);
    await userProfileStore.fetchUserProfileList({ page: 1, size: 9999 });
    console.log(userProfileStore.list);
    if (props.type === "edit") {
      Object.assign(form, props.form);
    }
  } else {
    Object.assign(form, cloneDeep(defaultForm));
  }
});

function handleConfirm() {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      if (props.type === "add") {
        await rechargeStore.fetchCreateRecharge(form);
      } else {
        await rechargeStore.fetchUpdateRecharge(form);
      }
      visible.value = false;
      emits("confirm");
    }
  });
}
</script>

<style scoped lang="scss"></style>
